<?php

namespace App\Console\Commands;

use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Console\Command;
use App\Models\User;
use App\Models\GoodsBrowse;
use App\Jobs\GenerateUserGoodsBrowse as BaseUserGoodsBrowse;



class GenerateUserGoodsBrowse extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'szsmkj:GenerateUserGoodsBrowse';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '生成用户每日浏览数据';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $goodsBrowse = GoodsBrowse::whereDate('created_at' , now())->first();
        $users = User::query()->get();

        if (!$goodsBrowse){
            foreach ($users as $v) {
                try{
                    dispatch(new BaseUserGoodsBrowse($v , 0));
                }catch(AuthorizationException $e){
                    \Log::info($v);
                    \Log::info('12333333333333333333333');
                    \Log::info($e->getMessage());
                }
            }
        }else{
            exec('php artisan szsmkj:SaveGoodsBrowseCommand > ./dev 2>&1');
        }
    }
}
